package thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

//实现一个固定线程个数的线程池
class MyThreadpool{
    private BlockingQueue<Runnable> queue=null;
    public MyThreadpool(int n){
        queue=new LinkedBlockingQueue<>(1000);
        for (int i = 0; i < n; i++) {
            Thread thread=new Thread(()->{
                try{
                    while (true){
                        Runnable task=queue.take();
                        task.run();
                    }
                }catch (InterruptedException e){
                    throw new RuntimeException(e);
                }
            });
            thread.start();
        }
    }
    public void submit(Runnable task)throws InterruptedException{
        queue.put(task);
    }
}

public class Demo35 {
    public static void main(String[] args) throws InterruptedException{
        MyThreadpool myThreadpool = new MyThreadpool(10);
        for (int i = 0; i < 1000; i++) {

            int id=i;
            myThreadpool.submit(() -> {
                System.out.println(Thread.currentThread().getName() + " id" + id);
            });
        }
    }
}